package com.ruoyi.web.controller.test;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.test.domain.FileRecord;
import com.ruoyi.test.service.IFileRecordService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件记录Controller
 *
 * @author KevenPotter
 * @date 2024-06-12 13:54:09
 */
@RestController
@AllArgsConstructor
@RequestMapping("/file/record")
public class FileRecordController extends BaseController {

    private final IFileRecordService fileRecordService;
    private final TokenService tokenService;

    /**
     * 查询文件记录列表
     */
    @PreAuthorize("@ss.hasPermi('file:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileRecord fileRecord) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<FileRecord> list = fileRecordService.selectFileRecordList(fileRecord);
        list.forEach(item -> fileRecordService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 导出文件记录列表
     */
    @PreAuthorize("@ss.hasPermi('file:record:export')")
    @Log(title = "文件记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileRecord fileRecord) {
        List<FileRecord> list = fileRecordService.selectFileRecordList(fileRecord);
        ExcelUtil<FileRecord> util = new ExcelUtil<FileRecord>(FileRecord.class);
        util.exportExcel(response, list, "文件记录数据");
    }

    /**
     * 获取文件记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('file:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        FileRecord fileRecord = fileRecordService.getById(id);
        fileRecord = fileRecordService.filterOutbound(fileRecord, loginUser);
        return success(fileRecord);
    }

    /**
     * 新增文件记录
     */
    @PreAuthorize("@ss.hasPermi('file:record:add')")
    @Log(title = "文件记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileRecord fileRecord) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        long start = System.currentTimeMillis();
        String filePath = RuoYiConfig.getUploadPath();
        String sourceFileUrl = fileRecord.getSourceFileUrl();
        String fileAddress = filePath + sourceFileUrl.replaceAll("/profile/upload", "");
        String fileName = sourceFileUrl.substring(sourceFileUrl.lastIndexOf("/") + 1, sourceFileUrl.lastIndexOf("."));
        String svgFileAddress = FileUtils.dwg2svg(fileAddress, fileName);
        String svgFileAddressNoWM = FileUtils.rfSvgWatermark(svgFileAddress);
        long end = System.currentTimeMillis();
        fileRecord.setFileName(fileName).setConvertFile1(svgFileAddressNoWM);
        fileRecord.setTimeConsuming(end - start);
        fileRecord = fileRecordService.filterInbound(fileRecord, loginUser);
        int i = fileRecordService.insertFileRecord(fileRecord);
        return toAjax(i);
    }

    /**
     * 修改文件记录
     */
    @PreAuthorize("@ss.hasPermi('file:record:edit')")
    @Log(title = "文件记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileRecord fileRecord) {
        return toAjax(fileRecordService.updateFileRecord(fileRecord));
    }

    /**
     * 删除文件记录
     */
    @PreAuthorize("@ss.hasPermi('file:record:remove')")
    @Log(title = "文件记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fileRecordService.deleteFileRecordByIds(ids));
    }
}
